package com.itdlc.android.library.utils;

import android.content.Context;
import android.os.Build;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.Log;

public class APPDidUtil {
    private static Context mContext;
    private static String mFolderName;
    private static String mClientNum;
    private static String mProjectNum;
    private static final String TAG = "APPDidUtil";

    public APPDidUtil() {
    }

    public static void init(Context context, String folderName, String clientNum, String projectNum) {
        mContext = context;
        mFolderName = folderName;
        mClientNum = clientNum;
        mProjectNum = projectNum;
        SPUtil.initSP(context, folderName);
    }

    public static String getAppDid() {
        String appDid = "ERR";
        appDid = SPUtil.getSharedString(mFolderName + "AppDid");
        if (!TextUtils.isEmpty(appDid)) {
            Log.e("APPDidUtil", "getAppDid--从SP中获取 ");
            return appDid;
        }
        appDid = SDCardUtil.getFromSDCard(mFolderName, "AppDid");
        if (!TextUtils.isEmpty(appDid)) {
            Log.e("APPDidUtil", "getAppDid--从SD卡获取 ");
            return appDid;
        }
        appDid=createAPPDid();
        Log.e("APPDidUtil", "自己生成__AppDid:"+appDid);
        if(TextUtils.equals("666c55330f2f888b5451966d",appDid)){
            appDid="666c55330f2f88451966d8b5";
            Log.e("APPDidUtil", "重置:"+appDid);
            saveAppDid(appDid);
        }
        return appDid;
    }

    private static String createAPPDid() {
        String appDid = null;
        appDid = Settings.Secure.getString(mContext.getContentResolver(), "android_id");
        Log.e("APPDidUtil", "自己生成 AppDid---" + appDid);
        if (TextUtils.isEmpty(appDid)) {
            appDid = Build.SERIAL.trim();
        }

        if (appDid == null) {
            appDid = Installation.id(mContext);
            if (appDid.contains("_")) {
                appDid.replaceAll("_", "");
            }
        }

        appDid = mClientNum + mProjectNum + appDid;
        appDid = appDid.replaceAll("\\s*", "");
        Log.e("appDid--appDid", appDid);
        if (appDid.length() >= 32) {
            appDid = appDid.substring(0, 32);
        }
        saveAppDid(appDid);
        return appDid;
    }

    public static void saveAppDid(String appDid) {
        if (appDid != null) {
            SPUtil.setSharedString(mFolderName + "AppDid", appDid);
            SDCardUtil.saveToSDCard(mFolderName, "AppDid", appDid);
        }

    }

    public static void saveTarget(String folderName, String fileName, String content) {
        SPUtil.setSharedString(folderName + fileName, content);
        SDCardUtil.saveToSDCard(folderName, fileName, content);
    }

    public static String getTarget(String folderName, String fileName) {
        String target = "ERR";
        target = SPUtil.getSharedString(folderName + fileName);
        if (target != null) {
            return target;
        } else {
            target = SDCardUtil.getFromSDCard(folderName, fileName);
            return target;
        }
    }
}
